/*
 * BanBu_DestoryAcountViewController
 * @description 本文件提供删除账户界面的ViewController
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供删除账户界面的ViewController
 */

#import "BanBu_DestoryAcountViewController.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_NavButton.h"
#import "AppDataManager.h"
#import "AppCommunicationManager.h"
#import "BanBu_ListViewController.h"
#include "BanBu_BroadcastTVC.h"
#import "UIViewController+Animation.h"
@interface BanBu_DestoryAcountViewController ()
@property (nonatomic,strong)UITextField *passInput;
@end

@implementation BanBu_DestoryAcountViewController
-(void)viewWillAppear:(BOOL)animated
{
    [UIView setAnimationsEnabled:YES];
    [super viewWillAppear:animated];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
-(void)dealloc
{
    [AppComManager cancalHandlesForObject:self];
    [super dealloc];
}
-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
//    [self dismissViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    
    self.title = NSLocalizedString(@"sysDestroyAccountTitle", nil);
    
    //相册名称Label和输入相册名称textField
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __MainScreen_Width, __MainScreen_Height-44)];
    [self.view addSubview:backView];
    
    UILabel *psssword = [[UILabel alloc] initWithFrame:CGRectMake(10, 20,__MainScreen_Width-20,40)];
    psssword.textAlignment = kTextAlignmentLeft;
    psssword.backgroundColor = [UIColor clearColor];
    psssword.text = [NSString stringWithFormat:@"%@ :",NSLocalizedString(@"密码", nil)];
    psssword.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16];
    [backView addSubview:psssword];
    
    //    viewHeight = viewHeight+40;
    
    
    _passInput = [[UITextField alloc] initWithFrame:CGRectMake(110, 20,200,40)];
    _passInput.textAlignment = kTextAlignmentLeft;
    _passInput.backgroundColor = [UIColor colorWithPatternImage:[ThemeManager getThemeImage:@"tablecell_backimage.png"]];
    _passInput.delegate = self;
    _passInput.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _passInput.font = [UIFont systemFontOfSize:16];
    _passInput.borderStyle = UITextBorderStyleRoundedRect;
    _passInput.textColor = [UIColor colorWithRed:50.0/255 green:79.0/255 blue:133.0/255 alpha:1.0];
    _passInput.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passInput.returnKeyType = UIReturnKeyNext;
    _passInput.placeholder  = NSLocalizedString(@"errorMsg2", nil);
    [backView addSubview:_passInput];
    [_passInput release];
    
    UIButton *destoryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [destoryBtn setBackgroundImage:[[UIImage imageNamed:@"btn_red.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [destoryBtn setTitle:NSLocalizedString(@"sysDestroyAccountTitle", nil) forState:UIControlStateNormal];
    [destoryBtn setFrame:CGRectMake(35, 90, 250, 45)];
    [destoryBtn addTarget:self action:@selector(destory) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:destoryBtn];
    
    
    CGSize size = [NSLocalizedString(@"sysDestroyAccountAbout", nil) sizeWithFont:[UIFont boldSystemFontOfSize:16] constrainedToSize:CGSizeMake(300, 1000) lineBreakMode:kTextLineBreakByWordWrapping];
    

    
    UILabel *helpLabel= [[UILabel alloc] initWithFrame:CGRectMake(10, 160, 300, size.height)];
    helpLabel.font = [UIFont boldSystemFontOfSize:16];
    helpLabel.backgroundColor = [UIColor clearColor];
    helpLabel.textAlignment = kTextAlignmentLeft;
    helpLabel.numberOfLines = 0;
    helpLabel.lineBreakMode = kTextLineBreakByWordWrapping;
    helpLabel.textColor = [UIColor darkGrayColor];
    
    helpLabel.text = NSLocalizedString(@"sysDestroyAccountAbout", nil);
    [backView addSubview:helpLabel];
    
    [helpLabel release];
    

}
-(void)destory
{
    if(_passInput.text.length<6)
    {
        [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"errorMsg3", nil) activityAnimated:NO duration:1];
        return;
        
    }
    [TKLoadingView showTkloadingAddedTo:self.navigationController.view title:NSLocalizedString(@"chuliNotice", nil) activityAnimated:YES];
    NSMutableDictionary *par = [NSMutableDictionary dictionaryWithObjectsAndKeys:_passInput.text,@"password",nil];
    self.navigationController.view.userInteractionEnabled = NO;
    [AppComManager getBanBuData:BanBu_Set_Account_Destroy par:par delegate:self];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
-(void)banbuRequestDidFinished:(NSDictionary *)resDic error:(NSError *)error
{
    self.navigationController.view.userInteractionEnabled = YES;
    [TKLoadingView dismissTkFromView:self.navigationController.view animated:NO afterShow:0];
    if(error)
    {
        [TKLoadingView showTkloadingAddedTo:self.view title:NSLocalizedString(@"network_error", nil) activityAnimated:NO duration:1];
    }
    else
    {
        if([[resDic valueForKey:@"ok"] boolValue])
        {
            
            if([AppComManager.receiveMsgTimer isValid])
            {
                [AppComManager.receiveMsgTimer invalidate];
                
                AppComManager.receiveMsgTimer=nil;
            }
            [AppComManager stopReceiveMsgForUid:nil];
            [AppComManager.receiveMsgQueue cancelAllOperations];
            
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithDictionary:[UserDefaults valueForKey:[UserDefaults valueForKey:@"myID"]]];
            [dic removeObjectForKey:@"loginid"];
            [MyAppDataManager.talkPeoples removeAllObjects];
            [MyAppDataManager.secondaryTalkPeople removeAllObjects];
            [MyAppDataManager.dialogs removeAllObjects];
            
            MyAppDataManager.useruid = @"";
            [UserDefaults setObject:dic forKey:[UserDefaults valueForKey:@"myID"]];
            [UserDefaults synchronize];
            [dic release];

     
            AppComManager.isReceivingDialods = NO;
            

            [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
            BanBu_ListViewController *acontrol = (BanBu_ListViewController *)[self.tabBarController.viewControllers objectAtIndex:0];
            acontrol.currentPage = 0;
            BanBu_BroadcastTVC *bcontrol = (BanBu_BroadcastTVC *)[self.tabBarController.viewControllers objectAtIndex:0];
            bcontrol.DosPage = 0;
            
            
            NSArray *guanxi = [NSArray arrayWithObjects:@"g",@"f",@"h", nil];
            
            for (int i=0; i<guanxi.count; i++)
            {
                NSString *path = [DataCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@-%@listdata",MyAppDataManager.useruid,[guanxi objectAtIndex:i]]];
                [FileManager removeItemAtPath:path error:nil];
            }
            
            [UserDefaults synchronize];
            
            [MyAppDataManager.friendsDos removeAllObjects];
            [MyAppDataManager.contentArr removeAllObjects];
            [MyAppDataManager.friendViewList removeAllObjects];
            [MyAppDataManager.friends removeAllObjects];
            
            NSMutableDictionary *uidDic = [NSMutableDictionary dictionaryWithDictionary:[UserDefaults valueForKey:MyAppDataManager.useruid]];
            [uidDic removeObjectForKey:@"loginid"];
            [UserDefaults setValue:uidDic forKey:MyAppDataManager.useruid];
            [UserDefaults synchronize];
            
            BanBu_AppDelegate *delegate = (BanBu_AppDelegate *)[UIApplication sharedApplication].delegate;
            [delegate setViewController:NO];
            [AppComManager.networkQueue cancelAllOperations];
            [self.navigationController popToRootViewControllerAnimated:NO];
//            [self pop];
//            [self performSelector:@selector(pop) withObject:nil afterDelay:2];
            
//            [((UIViewController *)self.settingDelegate).navigationController popViewControllerAnimated:YES];
        }
    }
}
@end
